package com.baozun.util;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 
 * @Title:
 * @Company:宝尊电子商务有限公司
 * @Description:
 * @Author:xiaozhou.zhou
 * @Since:2015年1月9日
 * @Copyright:Copyright (c) 2014
 * @ModifyDate:
 * @Version:1.1.0
 */
public class FileUtil {

    /**
     * base64字符串转成文件
     * 
     * @param path
     * @param fileName
     * @param base64
     * @return
     * @Description:
     */
    public static File base64ToFile(String path, String fileName, String base64) {
        File file = null;
        File filePath = null;
        FileOutputStream out = null;
        try {
            // 解码，然后将字节转换为文件
            filePath = new File(path);
            file = new File(path + "/" + fileName);
            if (!filePath.exists()) {
                filePath.mkdirs();
            }
            if (!file.exists()) {
                file.createNewFile();
            }
            byte[] bytes = BASE64Util.decode(base64);// 将字符串转换为byte数组
            ByteArrayInputStream in = new ByteArrayInputStream(bytes);
            byte[] buffer = new byte[1024];
            out = new FileOutputStream(file);
            int byteread = 0;
            while ((byteread = in.read(buffer)) != -1) {
                out.write(buffer, 0, byteread); // 文件写操作
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return file;
    }

    /**
     * 将文件转成字节数组
     * 
     * @param file
     * @return
     */
    public static byte[] file2ByteArray(File file) {
        byte[] ret = null;
        try {
            if (file == null) {
                // log.error("helper:the file is null!");
                return null;
            }
            FileInputStream in = new FileInputStream(file);
            ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
            byte[] b = new byte[4096];
            int n;
            while ((n = in.read(b)) != -1) {
                out.write(b, 0, n);
            }
            in.close();
            out.close();
            ret = out.toByteArray();
        } catch (IOException e) {
            // log.error("helper:get bytes from file process error!");
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 字节数组转成文件
     * 
     * @param byteArray
     * @param file
     * @return
     */
    public static File byteArray2File(byte[] byteArray, String file) {
        File ret = null;
        BufferedOutputStream stream = null;
        try {
            ret = new File(file);
            FileOutputStream fstream = new FileOutputStream(ret);
            stream = new BufferedOutputStream(fstream);
            stream.write(byteArray);
        } catch (Exception e) {
            // log.error("helper:get file from byte process error!");
            e.printStackTrace();
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e) {
                    // log.error("helper:get file from byte process error!");
                    e.printStackTrace();
                }
            }
        }
        return ret;
    }

    /**
     * 
     * @param file 待复制的文件
     * @param filePath 目标路径
     * @param fileName 目标文件名
     * @param isDelete 是否删除源文件
     */
    public static void copyFile(File file, String filePath, String fileName, boolean isDelete) {
        // TODO Auto-generated method stub
        File directory = new File(filePath);
        if (!directory.exists()) {
            directory.mkdirs();
        }
        byte[] byteArray = file2ByteArray(file);
        byteArray2File(byteArray, filePath + "/" + fileName);
        if (isDelete) {
            file.delete();
        }
    }

    /**
     * 文件转成base64编码
     * @param file
     * @return
     * @Description:
     */
    public static String file2Base64(File file) {
        // TODO Auto-generated method stub
        String base64 = "";
        try {
            FileInputStream fis = new FileInputStream(file);
            byte[] buffer = new byte[(int) file.length()];
            fis.read(buffer);
            fis.close();
            base64 = BASE64Util.encode(buffer);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return base64;
    }

    /**
     * 删除目录下所有文件
     * 
     * @param derictory
     * @Description:
     */
    public static void deleteDirectory(String directory) {
        File dir = new File(directory);
        deleteDirectory(dir);
    }

    /**
     * 删除目录下所有文件
     * 
     * @param dir
     * @Description:
     */
    public static void deleteDirectory(File dir) {
        if (dir.exists() && dir.isDirectory()) {
            File[] files = dir.listFiles();
            for (File file : files) {
                if (file.isDirectory()) {
                    deleteDirectory(dir);
                } else {
                    deleteFile(file);
                }
            }
            deleteFile(dir);
        }
    }

    /**
     * 删除指定目录文件
     * 
     * @param filePath
     * @Description:
     */
    public static void deleteFile(String filePath) {
        File file = new File(filePath);
        deleteFile(file);
    }

    /**
     * 删除文件
     * 
     * @param file
     * @Description:
     */
    public static void deleteFile(File file) {
        if (file.exists()) {
            file.delete();
        }
    }

    /**
     * 将输入流写进文件中
     * 
     * @param inputStream
     * @param temp
     * @Description:
     */
    public static void stream2File(InputStream inputStream, File temp) {
        // 将流写入文件
        try {
            FileOutputStream fos = new FileOutputStream(temp);
            byte buffer[] = new byte[4 * 1024];
            while ((inputStream.read(buffer)) != -1) {
                fos.write(buffer);
            }
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    /**
     * 将输入流写入指定目录中
     * 
     * @param inputStream
     * @param filePath
     * @param fileName
     * @return
     * @Description:如果文件目录没有生成，将生成目录
     */
    public static File stream2File(InputStream inputStream, String filePath, String fileName) {
        // 将流写入文件
        File filePa = new File(filePath);
        if (!filePa.exists()) {
            filePa.mkdirs();
        }
        File file = new File(filePath + "/" + fileName);
        stream2File(inputStream, file);
        return file;
    }
}
